if (typeof String.prototype.trim == 'undefined')
	String.prototype.trim = function() {
		return this.replace(/(^\s*)|(\s*$)/g, '');
	};
/**
 * 验证超类
 */
var Validation = Class.create();
Validation.prototype = {
	initialize : function() {
		this.id = null;
		this.message = null;
		this.error = null;
		this.min = 1;
		this.max = 2147483647;
		this.required = true;
		this.enable = true;
		this.params = new Array();

	},
	validate : function() {
		if (this.enable) {
			var value = $F(this.id).trim();
			if (!this.required && value.length == 0) {
			} else {
				return this.matching(value);
			}
		}
		return true;
	},
	matching : function(value) {
		return false;
	}
};

/**
 * 非空验证:String不为null并且在去除在String两端的空字符(如果存在)后, String长度仍要大于0.
 */
var RequiredValidation = Class.create();
RequiredValidation.prototype = Object.extend(new Validation(), {
	initialize : function(id, message, error, min, max, enable) {
		this.id = id;
		this.message = message;
		this.error = error;
		if (arguments.length > 3) {
			this.min = min;
			this.max = max;
			this.enable = enable;
		}
	},
	validate : function() {
		if (this.enable) {
			var value = $F(this.id).trim();
			return this.min <= value.length && value.length <= this.max;
		}
		return true;
	}
});

/**
 * 邮件地址验证
 */
var EmailValidation = Class.create();
EmailValidation.prototype = Object.extend(new Validation(), {
	initialize : function(id, message, error, required, enable) {
		this.id = id;
		this.message = message;
		this.error = error;
		if (arguments.length > 3) {
			this.required = required;
			this.enable = enable;
		}
	},
	matching : function(value) {
		return /^[^\s()<>@,;:\/]+@\w[\w\.-]+\.[a-z]{2,}$/i.test(value);
	}
});

/**
 * 电话号码验证 000-0000000 0000-0000000 000-00000000 0000-00000000
 */
var PhoneNumberValidation = Class.create();
PhoneNumberValidation.prototype = Object
		.extend(
				new Validation(),
				{
					initialize : function(id, message, error, required, enable) {
						this.id = id;
						this.message = message;
						this.error = error;
						if (arguments.length > 3) {
							this.required = required;
							this.enable = enable;
						}
					},
					matching : function(value) {
						// return (!value.isEmpty() &&
					// /^\(?[2-9]\d{2}[\)\.-]?\s?\d{3}[\s\.-]?\d{4}$/.test(value.trim()));
					return /(^[0-9]{3,4}\-[0-9]{7,8}$)|(^[0-9]{7,8}$)|(^\([0-9]{3,4}\)[0-9]{3,8}$)|(^0{0,1}13[0-9]{9}$)/
							.test(value);
				}
				});

/**
 * 日期验证 yyyy mm dd, yyyy/mm/dd, yyyy.mm.dd, yyyy-mm-dd
 */
var DateValidation = Class.create();
DateValidation.prototype = Object.extend(new Validation(), {
	initialize : function(id, message, error, required, enable) {
		this.id = id;
		this.message = message;
		this.error = error;
		if (arguments.length > 3) {
			this.required = required;
			this.enable = enable;
		}
	},
	matching : function(value) {
		var rxp = /^(\d{4})[\s\.\/-](\d{1,2})[\s\.\/-](\d{1,2})$/;
		if (rxp.test(value)) {
			var result = value.match(rxp);
			var y = result[1];
			var m = result[2];
			var d = result[3];

			if (m < 1 || m > 12 || y < 1900 || y > 2100) {
				return false;
			}
			if (m == 2) {
				var days = ((y % 4) == 0) ? 29 : 28;
			} else {
				if (m == 4 || m == 6 || m == 9 || m == 11) {
					var days = 30;
				} else {
					var days = 31;
				}
			}
			return (d >= 1 && d <= days);
		} else {
			return false;
		}
	}
});

/**
 * 网址验证
 */
var URLValidation = Class.create();
URLValidation.prototype = Object.extend(new Validation(), {
	initialize : function(id, message, error, required, enable) {
		this.id = id;
		this.message = message;
		this.error = error;
		if (arguments.length > 4) {
			this.required = required;
			this.enable = enable;
		}
	},
	matching : function(value) {
		return /http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?/.test(value);
	}
});

/**
 * 数值验证:String仅为以非0开始的数值,String可以精确到小数点后两位
 */
var NumericalValidation = Class.create();
NumericalValidation.prototype = Object.extend(new Validation(), {
	initialize : function(id, message, error, required, enable) {
		this.id = id;
		this.message = message;
		this.error = error;
		if (arguments.length > 3) {
			this.required = required;
			this.enable = enable;
		}
	},
	matching : function(value) {
		return /^(([1-9]\d*)|0)(\.\d{1,2})?$/.test(value);
	}
});

/**
 * 英文字母验证:String 不包含除英文字母a-zA-Z以外的任何字符.
 */
var AlphaValidation = Class.create();
AlphaValidation.prototype = Object.extend(new Validation(), {
	initialize : function(id, message, error, min, max, required, enable) {
		this.id = id;
		this.message = message;
		this.error = error;
		if (arguments.length > 3) {
			this.min = min;
			this.max = max;
			this.required = required;
			this.enable = enable;
		}
	},
	matching : function(value) {
		return !/[^a-zA-Z]/g.test(value) && this.min <= value.length
				&& value.length <= this.max;
	}
});

/**
 * 英文字母及数字验证:String 不包含除英文字母a-zA-Z及数字0-9以外的任何字符.
 */
var AlphaNumericValidation = Class.create();
AlphaNumericValidation.prototype = Object.extend(new Validation(), {
	initialize : function(id, message, error, min, max, required, enable) {
		this.id = id;
		this.message = message;
		this.error = error;
		if (arguments.length > 3) {
			this.min = min;
			this.max = max;
			this.required = required;
			this.enable = enable;
		}
	},
	matching : function(value) {
		return !/[^a-zA-Z0-9-]/g.test(value) && this.min <= value.length
				&& value.length <= this.max;
	}
});

/**
 * 汉字验证
 */
var ChineseValidation = Class.create();
ChineseValidation.prototype = Object.extend(new Validation(), {
	initialize : function(id, message, error, min, max, required, enable) {
		this.id = id;
		this.message = message;
		this.error = error;
		if (arguments.length > 3) {
			this.min = min;
			this.max = max;
			this.required = required;
			this.enable = enable;
		}
	},
	matching : function(value) {
		return /^[\u0391-\uFFE5]+$/.test(value) && this.min <= value.length
				&& value.length <= this.max;
	}
});

/**
 * 比较验证
 */
var CompareValidation = Class.create();
CompareValidation.prototype = Object.extend(new Validation(), {
	initialize : function(id, message, error, compareTo, enable) {
		this.id = id;
		this.message = message;
		this.error = error;
		this.params[0] = compareTo;
		if (arguments.length > 4) {
			this.enable = enable;
		}
	},
	validate : function() {
		if (this.enable) {
			return this.matching($F(this.id), $F(this.params[0]));
		}
		return true;
	},
	matching : function(value1, value2) {
		return value1.trim() == value2.trim();
	}
});

/**
 * 组织机构代码验证
 * */
var OrganizationCodeValidation = Class.create();
OrganizationCodeValidation.prototype = Object.extend(new Validation(), {
	initialize : function(id, message, error, required, enable) {
		this.id = id;
		this.message = message;
		this.error = error;
		if (arguments.length > 3) {
			this.required = required;
			this.enable = enable;
		}
	},
	matching : function(value) {
		return /^[A-Za-z0-9]{8}-[A-Za-z0-9]{1}/.test(value);
	}
});

/**
 * 验证器
 */
var EmapValidator = Class.create();
EmapValidator.prototype = {
	initialize : function(form) {
		this.validations = new Array();
		this.form = $(form);
		this.form.onsubmit = this.submit.bindAsEventListener(this);
		this.form.onreset = this.reset.bindAsEventListener(this);
	},
	add : function(validation) {
		if (validation instanceof Validation) {
			var id = validation.id;
			var element = $(id);
			if (element.type == 'text' || element.type == 'password')
				element.maxLength = validation.max;
			element.onblur = this.handleMessage.bindAsEventListener(this);
			element.onfocus = this.handleMessage.bindAsEventListener(this);
			new Insertion.After(id, this.setTip(validation));
			this.validations[id] = validation;
		}
	},
	remove : function(id) {
		this.validations[id] = null;
	},
	enable : function(id) {
		this.validations[id].enable = true;
	},
	disable : function(id) {
		this.validations[id].enable = false;
	},
	handleMessage : function(event) {
		var id = Event.element(event).id;
		var eventName = event.type;
		var validation = this.validations[id];
		var element = $(id + '_Tip');
		if (eventName == 'focus') {
			element.className = 'message';
			element.innerHTML = validation.message;
		} else if (eventName == 'blur') {
			if(!validation.validate()){
				element.className = 'error';
				element.innerHTML = validation.error;
			}
		}
	},
	setTip : function(validation) {
		var msg = validation.message;
		var err = validation.error;
		msg = (msg == null) ? "&nbsp;" : "&nbsp;" + msg;
		err = (err == null) ? "&nbsp;" : "&nbsp;" + err;
		validation.message = msg;
		validation.error = err;
		return "<span class='message' id='" + validation.id + "_Tip'>" + msg
				+ "</span>";
	},
	prepare : function() {
	},
	submit : function() {
		this.prepare();
		var cansubmit = true;
		for (id in this.validations) {
			var validation = this.validations[id];
			var element = $(id);
			if (validation instanceof Validation) {
				if (!validation.validate()) {
					element.focus();
					element.blur();
					cansubmit = false;
				}
			}
		}
		return cansubmit;
	},
	reset : function() {
		for (id in this.validations) {
			var validation = this.validations[id];
			if (validation instanceof Validation) {
				var element = $(id + '_Tip');
				element.className = 'message';
				element.innerHTML = validation.message;
			}
		}
	}
};